﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using Vlc.DotNet.Core; 

namespace VideoHandler
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //rtsp://admin:3x!B6D9C372@175.162.12.176:554/channel1
        private Process process;
        private string rtspUrl = "rtsp://admin:3x!B6D9C372@192.168.23.13:554/channel1";
        private string hlsUrl = "D:\\tmp\\opt\\output\\output.m3u8";
        private string vlcPath = "D:\\Program Files\\VideoLAN\\VLC";
        private DispatcherTimer timer = new DispatcherTimer();

        public MainWindow()
        {
            InitializeComponent();

            string ffmpegPath = Path.Combine(@"D:\tmp\opt\ffmpeg", "ffmpeg.exe");
            this.vlcControl.SourceProvider.CreatePlayer(new DirectoryInfo(vlcPath));
         
            ConvertRtspToHls(ffmpegPath, rtspUrl);
            //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri(hlsUrl));
            //timer.Interval = TimeSpan.FromSeconds(5);
            //timer.Tick += Timer_Tick;
            //timer.Start();
            Closed += MainWindow_Closed;
        }

        private void MainWindow_Closed(object? sender, EventArgs e)
        {
            process?.Kill();
            process?.Close();
            this.vlcControl.SourceProvider.MediaPlayer.Stop();
            this.vlcControl.SourceProvider.Dispose();
            base.OnClosed(e);
        }

        private void Timer_Tick(object? sender, EventArgs e)
        {
            //this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri("rtsp://admin:3x!B6D9C372@192.168.23.13:554/channel1"));
            if (this.vlcControl.SourceProvider.MediaPlayer.State != Vlc.DotNet.Core.Interops.Signatures.MediaStates.Playing)
            {
               // this.vlcControl.SourceProvider.MediaPlayer.Play(new Uri(hlsUrl));
            }
        }

        private void ConvertRtspToHls(string ffmpegPath, string rtspUrl)
        {
            string outputDir = "D:\\Code\\ConsoleApp1\\APITest\\wwwroot";
            string outputPlaylist = Path.Combine(outputDir, "output.m3u8");
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = ffmpegPath,
                Arguments = $"-i {rtspUrl} -codec copy -hls_time 10 -hls_list_size 6 -f hls {outputPlaylist}",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            using (process = Process.Start(startInfo))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    //string result = reader.ReadLine();
                    //Console.WriteLine(result);
                }

                using (StreamReader errorReader = process.StandardError)
                {
                    //string errors = errorReader.ReadToEnd();
                    //Console.WriteLine(errors);
                }

                process.WaitForExit();

                //process.Close();
            }
        }

    }
}
